package cn.gem.rainbow.api;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * SpringBoot系列教程web篇之404、500异常页面配置
 * https://mp.weixin.qq.com/s?__biz=MzU3MTAzNTMzMQ==&mid=2247484336&idx=1&sn=d70f15e77bbd219af8015f9037a167fb&chksm=fce7181ccb90910aee427a3f3ed7660e8303c7460859c82622a651ce1cc3d7a97f62f80ed4e0&token=2447275&lang=zh_CN#rd
 */
@Controller
public class ViewFulController {

    /**
     * 1.注掉 RestNotFoundController
     * 2.访问http://localhost:8080/rainbow/divide
     * 会出现view风格的 static目录的 500错误页面
     *
     * @param sub
     * @return
     */
    @GetMapping(path = "/divide")
    public int divide(int sub) {
        System.out.println("divide1");
        return 1000 / sub;
    }


    @GetMapping("/th")
    public String testChineseCode(String name, Integer age, Model model) {

        String an = "name: " + name + " age: " + age;
        System.out.println(an);
        model.addAttribute("name", name);
        model.addAttribute("age", age);
        return "an";
    }


    @GetMapping(path = "/r3")
    public String r3() {
        return "redirect:/index2";
    }

    @GetMapping(path = {"/index2"})
    public ModelAndView index() {
        Map<String, Object> data = new HashMap<>(2);
        data.put("name", "YiHui Thymeleaf");
        data.put("now", LocalDateTime.now().toString());
        return new ModelAndView("index2", data);
    }


    /**
     * 一般不建议直接使用jdk的String.split来分割字符串，内部实现是根据正则来处理的，虽然更强大，但在简单的场景下，性能开销更大
     */

    private static String[] contents =

            ("绿蚁浮觞香泛泛，黄花共荐芳辰。\n清霜天宇净无尘。\n登高宜有赋，拈笔戏成文。\n可奈园林摇落尽，悲秋意与谁论。\n眼中相识几番新。\n龙山高会处，落帽定何人。").split("\n");

    private static Random random = new Random();


    @GetMapping(path = "show1")
    public String showOne(Model model) {
        model.addAttribute("title", "临江仙");
        model.addAttribute("content", contents[random.nextInt(6)]);
        return "show1";

    }


    @GetMapping(path = "show2")
    public String showTow(Map<String, Object> data) {
        data.put("name", "Show2---->");
        data.put("now", LocalDateTime.now().toString());
        return "show2";

    }


    /**
     * 视图风格的Controller，若配置了@ResponseBody,则不返回具体的视图页面内容，而是返回 json
     * {"code":1000,"msg":"操作成功","data":"view访问! {}","timestamp":1600567100708}
     * @param request
     * @return
     */
    @ResponseBody
    @GetMapping(path = "/view")
    public String index(HttpServletRequest request) {
        return "view访问! " + JSON.toJSONString(request.getParameterMap());
    }

    /**
     * 视图风格的Controller,配置重定向req
     * <p>
     * 请求 http://localhost:8080/rainbow/r1
     * 时状态码 302
     * 请求会被重定向到 http://localhost:8080/rainbow/view
     *
     * @return
     */
    @GetMapping(path = "/r1")
    public String r1() {
        return "redirect:/view";
    }
}
